package com.easylinkin.linkappapi.trainingpenetrate.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.penetsecuremanage.entity.RailLinkappGridManagementInfo;
import com.easylinkin.linkappapi.penetsecuremanage.mapper.RailLinkappGridManagementInfoMapper;
import com.easylinkin.linkappapi.roster.entity.RailLinkappRosterPersonnel;
import com.easylinkin.linkappapi.roster.mapper.LinkappRosterPersonnelMapper;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.trainingpenetrate.dto.RailLinkappSafetyEduDetailDTO;
import com.easylinkin.linkappapi.trainingpenetrate.dto.RailLinkappSafetyEduPlanDTO;
import com.easylinkin.linkappapi.trainingpenetrate.entity.RailLinkappSafetyEduDetail;
import com.easylinkin.linkappapi.trainingpenetrate.entity.RailLinkappSafetyEduPlan;
import com.easylinkin.linkappapi.trainingpenetrate.mapper.RailLinkappSafetyEduDetailMapper;
import com.easylinkin.linkappapi.trainingpenetrate.mapper.RailLinkappSafetyEduPlanMapper;
import com.easylinkin.linkappapi.trainingpenetrate.service.IRailLinkappSafetyEduPlanService;
import com.easylinkin.linkappapi.trainingpenetrate.vo.RailLinkappSafetyEduPlanVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lyk
 * @date 2025/7/1 下午 4:55
 */
@Service
public class RailLinkappSafetyEduPlanServiceImpl extends ServiceImpl<RailLinkappSafetyEduPlanMapper, RailLinkappSafetyEduPlan> implements IRailLinkappSafetyEduPlanService {
    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    @Autowired
    private LinkappRosterPersonnelMapper linkappRosterPersonnelMapper;

    @Autowired
    private RailLinkappGridManagementInfoMapper gridManagementInfoMapper;
    @Resource
    private CommonService commonService;
    @Autowired
    private RailLinkappSafetyEduDetailMapper safetyEduDetailMapper;
    @Override
    public IPage<RailLinkappSafetyEduPlanVO> selectPage(Page page, RailLinkappSafetyEduPlanDTO customQueryParams) {
        customQueryParams.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return baseMapper.selectPage(page,customQueryParams);
    }

    @Override
    public void add(RailLinkappSafetyEduPlanDTO dto) {
        //保存
        RailLinkappSafetyEduPlan one = new RailLinkappSafetyEduPlan();
        one.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        one.setName(dto.getName());
        one.setTrainingType(dto.getTrainingType());
        one.setTrainingSite(dto.getTrainingSite());
        one.setEduDate(dto.getEduDate());
        one.setNotes(dto.getNotes());
        one.setContent(dto.getContent());
        one.setCuratorId(dto.getCuratorId());
        //查询负责人名称
        RailLinkappRosterPersonnel personnel = linkappRosterPersonnelMapper.selectById(Long.valueOf(dto.getCuratorId()));
        if (Objects.nonNull(personnel)) {
            one.setCuratorName(personnel.getRealName());
        }
        //培训班组//网格
        if (StringUtils.isNotEmpty(dto.getGridId())) {
            one.setGridId(dto.getGridId());
            //查询网格
            RailLinkappGridManagementInfo railLinkappGridManagementInfo = gridManagementInfoMapper.selectById(Long.valueOf(dto.getGridId()));
            if (Objects.nonNull(railLinkappGridManagementInfo)) {
                one.setGridName(railLinkappGridManagementInfo.getGridName());
            }
        }
        one.setImgs(dto.getImgs());
        one.setFiles(dto.getFiles());
        one.setPassNumber(dto.getPassNumber());
        //保存新增人员内容
        commonService.setCreateAndModifyInfo(one);
        baseMapper.insert(one);
        //保存人员信息
        addDetails(one,dto.getAddList());
    }

    @Override
    public void edit(RailLinkappSafetyEduPlanDTO dto) {
        RailLinkappSafetyEduPlan railLinkappSafetyEduPlan = baseMapper.selectById(dto.getId());
        if (Objects.isNull(railLinkappSafetyEduPlan)){
            return;
        }
        railLinkappSafetyEduPlan.setName(dto.getName());
        railLinkappSafetyEduPlan.setTrainingType(dto.getTrainingType());
        railLinkappSafetyEduPlan.setTrainingSite(dto.getTrainingSite());
        railLinkappSafetyEduPlan.setEduDate(dto.getEduDate());
        if (StringUtils.isEmpty(dto.getCuratorId())){
            railLinkappSafetyEduPlan.setCuratorId("");
            railLinkappSafetyEduPlan.setCuratorName("");
        }else{
            if (!railLinkappSafetyEduPlan.getCuratorId().equals(dto.getCuratorId())){
                RailLinkappRosterPersonnel personnel = linkappRosterPersonnelMapper.selectById(Long.valueOf(dto.getCuratorId()));
                if (Objects.nonNull(personnel)) {
                    railLinkappSafetyEduPlan.setCuratorId(dto.getCuratorId());
                    railLinkappSafetyEduPlan.setCuratorName(dto.getCuratorName());
                }
            }
        }
        if (StringUtils.isEmpty(dto.getGridId())){
            railLinkappSafetyEduPlan.setGridId("");
            railLinkappSafetyEduPlan.setGridName("");
        }else{
            RailLinkappRosterPersonnel personnel = linkappRosterPersonnelMapper.selectById(Long.valueOf(dto.getCuratorId()));
            //查询网格
            RailLinkappGridManagementInfo railLinkappGridManagementInfo = gridManagementInfoMapper.selectById(Long.valueOf(dto.getGridId()));
            if (Objects.nonNull(railLinkappGridManagementInfo)) {
                railLinkappSafetyEduPlan.setGridId(dto.getGridId());
                railLinkappSafetyEduPlan.setGridName(railLinkappGridManagementInfo.getGridName());
            }
        }
        railLinkappSafetyEduPlan.setImgs(dto.getImgs());
        railLinkappSafetyEduPlan.setFiles(dto.getFiles());
        railLinkappSafetyEduPlan.setPassNumber(dto.getPassNumber());
        commonService.setModifyInfo(railLinkappSafetyEduPlan);
        baseMapper.updateById(railLinkappSafetyEduPlan);
        //保存人员信息
        editDetails(railLinkappSafetyEduPlan,dto.getAddList(),dto.getDelList());
    }
    public void editDetails(RailLinkappSafetyEduPlan one, List<RailLinkappSafetyEduDetailDTO> Adddetails, List<RailLinkappSafetyEduDetailDTO> delDetails){
        int sum = 0;
        int jgSum = 0;
        Date  now  = new Date();
        //删除
        if (Objects.nonNull(delDetails)){
           for (RailLinkappSafetyEduDetailDTO del:delDetails){
              if (Objects.nonNull(del.getId())){
                  safetyEduDetailMapper.deleteById(del.getId());
              }
           }
        }
        //新增 // 编辑
        for (RailLinkappSafetyEduDetailDTO add:Adddetails){
            if (Objects.nonNull(add.getId())){
                  //编辑
                RailLinkappSafetyEduDetail railLinkappSafetyEduDetail = safetyEduDetailMapper.selectById(add.getId());
                if (Objects.nonNull(railLinkappSafetyEduDetail)){
                    railLinkappSafetyEduDetail.setKhNumber(add.getKhNumber());
                    railLinkappSafetyEduDetail.setModifyTime(now);
                    safetyEduDetailMapper.updateById(railLinkappSafetyEduDetail);
                }
            }else{
                //新增
                RailLinkappSafetyEduDetail detail = new RailLinkappSafetyEduDetail();
                detail.setTenantId(one.getTenantId());
                detail.setEduId(String.valueOf(one.getId()));
                detail.setModifyTime(now);
                detail.setKhNumber(add.getKhNumber());
                detail.setPersonId(add.getPersonId());
                detail.setPersonName(add.getPersonName());
                detail.setPersonPhone(add.getPersonPhone());
                detail.setPersonStatus(add.getPersonStatus());
                if (Objects.nonNull(one.getPassNumber()) && Objects.nonNull(detail.getKhNumber())){
                    if (detail.getKhNumber()>=one.getPassNumber()){
                        detail.setIsQual(0);
                        jgSum++;
                    }else{
                        detail.setIsQual(1);
                    }
                }else{
                    detail.setIsQual(0);
                }
                safetyEduDetailMapper.insert(detail);
            }
        }
        //重新计算 参与考核人员 和 通过人数
        QueryWrapper<RailLinkappSafetyEduDetail> ros = new QueryWrapper<>();
        ros.eq("tenant_id",one.getTenantId());
        ros.eq("edu_id",one.getId());
        List<RailLinkappSafetyEduDetail> details = safetyEduDetailMapper.selectList(ros);
        if (details.size() == 0){
            one.setCompleteNumber(0);
            one.setPassThroughNumber(0);
        }else{
            for (RailLinkappSafetyEduDetail detail:details){
               if (Integer.valueOf(0).equals(detail.getIsQual())){
                   jgSum++;
               }
            }
        }
        one.setCompleteNumber(details.size());
        one.setPassThroughNumber(jgSum);
        baseMapper.updateById(one);
    }
    public void addDetails(RailLinkappSafetyEduPlan one, List<RailLinkappSafetyEduDetailDTO> details){
        int sum = 0;
        int jgSum = 0;
        Date  now  = new Date();
       for (RailLinkappSafetyEduDetailDTO dto:details){
           if (Objects.isNull(dto.getId())){
               RailLinkappSafetyEduDetail detail = new RailLinkappSafetyEduDetail();
               detail.setTenantId(one.getTenantId());
               detail.setEduId(String.valueOf(one.getId()));
               detail.setModifyTime(now);
               detail.setKhNumber(dto.getKhNumber());
               detail.setPersonId(dto.getPersonId());
               detail.setPersonName(dto.getPersonName());
               detail.setPersonPhone(dto.getPersonPhone());
               detail.setPersonStatus(dto.getPersonStatus());
               if (Objects.nonNull(one.getPassNumber()) && Objects.nonNull(detail.getKhNumber())){
                   if (detail.getKhNumber()>=one.getPassNumber()){
                       detail.setIsQual(0);
                       jgSum++;
                   }else{
                       detail.setIsQual(1);
                   }
               }else{
                   detail.setIsQual(0);
                   jgSum++;
               }
               safetyEduDetailMapper.insert(detail);
               sum++;
           }
       }
        one.setCompleteNumber(sum);
        one.setPassThroughNumber(jgSum);
        baseMapper.updateById(one);
    }


}
